package com.os.osframe.scaffold.create;

import com.os.osframe.scaffold.domain.ScaffoldXmlTable;
import com.os.osframe.scaffold.util.PropertyConfigurerLoader;
import com.os.osframe.util.DateUtil;
import com.os.osframe.util.StringUtil;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Created by wangdc on 2015-4-27.
 */
public class CreateIndexCode extends BaseCreate implements ICreateCode {

    public CreateIndexCode(){

    }
    protected List<ScaffoldXmlTable> scaffoldXmlTableList;
    public void create(String packages)throws Exception{
        System.out.println("=====开始生成首页控制器=====");
        //String path = replaceStr(new File("").getAbsolutePath(), "\\", "/");//获取项目的根路径，这里获取到的是softdp的路径
        String basePath = (String) getPropertyConfigurer().getProperty("osframe.file.path");//获取属性配置的服务器路径
        if (basePath.substring(basePath.length() - 1).equals("/")) {
            basePath = basePath.substring(basePath.length() - 1);
        }
        //System.out.println(path);//E:/java/intelijwork/softdp
        String targetFilePath=basePath;
        targetFilePath+="/auto/code/"+packages.replaceAll("\\.","/")+"/src/web";
        //System.out.println(targetFilePath);
        String tempPath = replaceStr(new File(this.getClass().getResource("").getPath()).getAbsolutePath(), "\\", "/");//获取当前类所在路径
        if(tempPath.indexOf("create")>-1){
            tempPath=tempPath.substring(0,tempPath.length()-7);
        }
        tempPath+="/index";
        //System.out.println(tempPath);
        //xmlObjList= ReadXmlObj.getDataInfoByModule(module, packages);//获取列表对象
        createFile(tempPath,targetFilePath);
    }
    protected void stringDeal(String packages){
        replaceStrings[0]="${packages}";
        replaceStrings[1]="${author}";
        replaceStrings[2]="${time}";
        replaceStrings[3]="${packageSimple}";
        replaceStrings[4]="${packagePath}";


        //获取域模型信息

        //获取包简称
        String packagesSimple=null;
        String[] packagesSimpleArr=packages.split("\\.");

        for(int i=0;i<packagesSimpleArr.length;i++){
            if(StringUtil.isNull(packagesSimple)){
                packagesSimple=packagesSimpleArr[i];
            }else{
                packagesSimple+=packagesSimpleArr[i].substring(0, 1).toUpperCase()+packagesSimpleArr[i].substring(1);
            }
        }
        targetStrings[0]=packages;
        targetStrings[1]=author;
        targetStrings[2]= DateUtil.convertDate2String(DateUtil.DATETIME_FORMAT, new Date());
        targetStrings[3]=packagesSimple;
        targetStrings[4]=packages.replaceAll("\\.","/");

    }
}
